const constants = require('./utils/constants')
const cpuStat = require('cpu-stat')
const getServerInfo = require('./utils/api').getServerInfo

class Agent {
    _axios;
    _localInfo = {
        //本地节点数量
        nodeNum: 1,
        //服务器IP
        serverIp: '',
        //本地节点信息
        nodeInfos: {
            node0: {
                pass: '',
                debugPort: '',
                infura: '',
                command: {
                    restartCommand: '',
                    backupCommand: '',
                    cleanDiskCommand: '',
                }
            }
        }
    }

    _data = {
        node0: {
            peers: [
                {
                    address: "06046d6f7fc5f1869587c5db84becfb1c7faf402dd50281d7e8051542578b6e6"
                },
                {
                    address: "067561c6ad47d908ad57b54ce680d5ad5d75ae993a4c005e84c0d10b30336319"
                },
            ],
            balance: {
                totalBalance: 0,
                availableBalance: 0
            },
            address: {
                overlay: '',
                underlay: [],
                ethereum: '',
                public_key: '',
                pss_public_key: ''
            }
        }
    }

    async init() {
        // await this.updateNodeInfo()
        this.updateServerCpu()

    }

    /**
     * 从远程服务器获取节点信息
     */
    async updateNodeInfo() {
        //获取服务器信息
        this._localInfo = await getServerInfo();
        //10分钟获取一次
        setInterval(
            async () => {
                this._localInfo = await getServerInfo();
            }, 10 * 60 * 1000)
    }

    /**
     * 5s更新一次服务器cpu
     */
    updateServerCpu() {
        setInterval(() => {
            cpuStat.usagePercent(function (err, percent) {
                if (!err) {
                    let cpuUsage = parseFloat(percent).toFixed(2);
                    console.log("CPU使用情况%=" + cpuUsage);
                }
            });
        }, 5 * 1000);
    }

    /**
     * 获取本地节点信息
     * @returns 
     */
    getData() {
        return this._data
    }
}

module.exports = Agent